package com.atguigu.school.service.impl;

import com.atguigu.school.commons.MD5;
import com.atguigu.school.commons.Page;
import com.atguigu.school.mapper.StudentMapper;
import com.atguigu.school.pojo.Student;
import com.atguigu.school.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    /**
     * 访问学生数据
     */
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void addOrUpdateStudent(Student student) {
        if (student.getId() == null || student.getId() == 0) {
            this.studentMapper.insertStudent(student);
        } else {
            //有更新密码
            if (student.getPassword() != null && student.getPassword().length() > 0) {
                student.setPassword(MD5.encrypt(student.getPassword()));
            }

            this.studentMapper.updateStudentById(student);
        }
    }

    @Override
    public void deleteStudentsByIds(List<Integer> ids) {
        this.studentMapper.deleteStudentsByIds(ids);
    }

    @Override
    public Page<Student> selectStudentsByNameAndClazzName(String name, String clazzName, Integer current, Integer size) {
        Integer total = this.studentMapper.countStudentsByNameAndClazzName(name, clazzName);
        Page<Student> page = new Page<>(current, size, total);

        List<Student> students = this.studentMapper.selectStudentsByNameAndClazzName(name, clazzName, page.getOffset(), size);
        page.setRecords(students);
        return page;
    }

}
